#include "crc16.h"

static const uint16_t CRC_Tab[16] = {
		0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
		0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
	};

uint16_t crc_CalcCCITT16NibbleVer(uint16_t initcrc, void *vptr, uint16_t len)
{
	uint8_t *ptr = (uint8_t *)vptr;
	uint16_t crc;
	uint8_t da;
	crc = initcrc;

	while( len-- != 0 )
	{
		da = ((uint8_t)(crc / 256)) / 16;
		crc <<= 4;
		crc ^= CRC_Tab[da ^ (*ptr / 16)];
		da = ((uint8_t)(crc / 256)) / 16;
		crc <<= 4;
		crc ^= CRC_Tab[da ^ (*ptr & 0x0f)];
		ptr++;
	}
	return(crc);
}